package Services;


import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public final class Mailman {

	public static void sendTheMail(String fromMail, String toMail, String userName, String Subject, String msg){
		Properties props = new Properties();
		Session session = Session.getDefaultInstance(props, null);

		String msgBody = msg;

		try {
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress(fromMail, "Donut Rush")); 
			String mrUser = userName;
			if (userName.equals("")){
				mrUser = "Mr. User";
			}
			message.addRecipient(Message.RecipientType.TO,
					new InternetAddress(toMail, mrUser));
			message.setSubject(Subject);
			message.setText(msgBody);
			Transport.send(message);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}



	@SuppressWarnings("deprecation")
	public static void sendVerMail(String fromMail, String toMail,
			String userName, String email, String pswd){
		
		 userName = java.net.URLEncoder.encode(userName);
		 String qString = "user=" + userName + "&email=" + email +"&pswd=" + pswd;
		
		String msgBody = "This is a verification mail sent by Donut Rush! app\n" +
		"please verify your registration by clicking the link below:\n" +
		"http://donutrush.appspot.com/EmailVer.do?" + qString;

		
		//DEBUG: change hard-coded email
		sendTheMail(fromMail, toMail, userName, "Donut Rush! Verification",msgBody);
	}
	
	public static void sendThanksMail(String fromMail, String toMail,
			String userName){
		
		String msgBody = "Thank you for registering to Donut Rush," +
				"for donations, please send us lots and lots of money!";
		
		//DEBUG: change hard-coded email
		sendTheMail(fromMail, toMail, userName, "Donut Rush! Verification",msgBody);
	}
}